package com.smartmedical.system.patient.controller;

import com.smartmedical.common.core.domain.HealthR;
import com.smartmedical.common.core.domain.R;
import com.smartmedical.common.security.utils.SecurityUtils;
import com.smartmedical.system.api.domain.PatientUsers;
import com.smartmedical.system.api.model.PatientUserJwt;

import com.smartmedical.system.patient.domain.dto.PatientIserArchivesDto;
import com.smartmedical.system.patient.domain.entity.PatientIserArchivesEntity;
import com.smartmedical.system.patient.domain.entity.PatientUserEntity;
import com.smartmedical.system.patient.domain.vo.EmailCodeVo;
import com.smartmedical.system.patient.domain.vo.UserInfoByIdVo;
import com.smartmedical.system.patient.service.PatientUserService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;

/**
 * @BelongsProject: smart-medical
 * @BelongsPackage: com.smartmedical.controller
 * @ClassName: UserController
 * @Author: zyn
 * @CreateTime: 2024-01-08  16:48
 * @Description: TODO
 */
@RestController
@RequestMapping("/user/v1")
public class PatientUserController {



    @Autowired
    private PatientUserService patientUserService;


    /**
     * 登录接口
     *
     * @param email 邮箱
     * @return 登录结果
     */
    @PostMapping("/login")
    public R<PatientUserJwt> login(@RequestParam String email){
        try {
            PatientUsers user = patientUserService.login(email);
            PatientUserJwt patientUserJwt = createPatientUserJwt(user);

            return R.ok(patientUserJwt);// R.ok() 是一个自定义的响应工具类，表示成功，可以根据实际需求进行定义
        } catch (Exception e) {
            return R.fail("Invalid email or password");  // 处理登录失败的情况
        }
    }
    private PatientUserJwt createPatientUserJwt(PatientUsers user) {
        PatientUserJwt patientUserJwt = new PatientUserJwt();
        patientUserJwt.setId(user.getId());
        patientUserJwt.setPhone(user.getPhone());
        patientUserJwt.setPwd(user.getPwd());
        patientUserJwt.setEmail(user.getEmail());
        patientUserJwt.setNickName(user.getNickName());
        patientUserJwt.setUserName(user.getUserName());
        patientUserJwt.setHeadPic(user.getHeadPic());
        patientUserJwt.setSex(user.getSex());
        patientUserJwt.setAge(user.getAge());
        patientUserJwt.setHeight(user.getHeight());
        patientUserJwt.setWeight(user.getWeight());
        patientUserJwt.setUpdateTime(user.getUpdateTime());
        patientUserJwt.setCreateTime(user.getCreateTime());
        patientUserJwt.setInvitationCode(user.getInvitationCode());
        patientUserJwt.setPatientUsers(user);

        return patientUserJwt;
    }


    @PostMapping("/sendOutEmailCode")
    public HealthR sendOutEmailCode(@RequestParam String email){

        HealthR patientUserR=  patientUserService.sendOutEmailCode(email);

        return HealthR.ok(patientUserR.getResult(),"发送成功");

    }
    /**
     * 注册接口
     *
     * @param email 邮箱
     * @param code 验证码
     * @param pwd1 密码
     * @param pwd2 确认密码
     * @param invitationCode 邀请码
     * @return 健康状态响应
     */
    @PostMapping("/register")
    public HealthR register(@RequestParam String email,
                            @RequestParam String code,
                            @RequestParam String pwd1,
                            @RequestParam String pwd2,
                             @RequestParam String invitationCode){
        // 调用 patientUserService 的 registerUser 方法进行用户注册
        HealthR healthR = patientUserService.registerUser(email, code, pwd1, pwd2, invitationCode);

        // 返回响应
        return HealthR.ok("","注册成功");
    }


    /**
     * 完善用户信息
     *
     * @param age 用户年龄
     * @param height 用户身高
     * @param weight 用户体重
     * @param id 用户ID
     * @return HealthR 健康状态响应
     */



    /**
     * 上传头像
     */

    @PostMapping("/uploadHeadPic")
    public HealthR uploadHeadPics(MultipartFile image){


        return patientUserService.uploadHeadPic(image);
    }










}
